// let pageSize = 5; //每页显示条数
// let currentPage = 1; //当前页
// let TotalPage;
var storeguanli = GetUser("storeguanli");
var management = GetUser("management");
//渲染
function xuanran(storeguanli,storeguanlipageSize,storeguanlicurrentPage) {
  let start = (storeguanlicurrentPage - 1) * storeguanlipageSize;
  let end = storeguanlicurrentPage * storeguanlipageSize;
  $("#userTable").empty();
  for (var i = start; i < end && i < storeguanli.length; i++) {
    tr = `
        <tr class="line">
        <td><input type="checkbox" data-id="${
          storeguanli[i].id
        }" class="cbxAll" ></td>
        <td>${i + 1}</td>
        <td>${storeguanli[i].storename}</td>
        <td>${storeguanli[i].fenlei}</td>
        <td>${storeguanli[i].storepinpai}</td>
        <td>${storeguanli[i].storepirce}</td>
        <td>${storeguanli[i].kuncun}</td>
        <td >
            <!-- 修改图标 -->
            <button data-id="${
              storeguanli[i].id
            }" type="button" class="layui-btn layui-btn-sm xiugai" ><i class="layui-icon"></i></button>
            <!-- 删除图标 -->
            <button data-id="${
              storeguanli[i].id
            }"  type="button" class="layui-btn layui-btn-normal layui-btn-sm cc" style="background-color: red;">
                <i class="layui-icon"></i></button>
        </td>
    </tr>
    `;
    $("#userTable").append(tr);
  }
}
BindPage(storeguanli);
AllChoose();
// AddPageEvent();
// Addstore()
//页数
function BindPage(Query) {
  let storeguanlipageSize = 5; //每页显示条数
  let storeguanlicurrentPage = 1; //当前页
  let TotalPage = Math.ceil(Query.length / storeguanlipageSize);
  if (storeguanlicurrentPage > TotalPage) {
    storeguanlicurrentPage = TotalPage;
  }
  if (TotalPage == 0) {
    storeguanlicurrentPage = 1;
  }
  //生成对应页码
  $("#divPage").html("");
  for (var i = 1; i <= TotalPage; i++) {
    $("#divPage").append(`<button class="pageBut">${i}</button>`);
    $(".pageBut").eq(0).addClass("cyan").siblings().removeClass("cyan");
    xuanran(storeguanli,storeguanlipageSize,storeguanlicurrentPage);
  }
  AddPageEvent();

//页码绑定事件
function AddPageEvent() {
  $("#divPage").on("click", ".pageBut", function () {
    storeguanlicurrentPage = parseInt($(this).text());
    $(this).addClass("cyan").siblings().removeClass("cyan");
    xuanran(storeguanli,storeguanlipageSize,storeguanlicurrentPage);
  });
}
}
//添加
function Addstore() {
  let id = Date.now();
  let storename = $("#storename").val();
  let storepinpai = $("#storepinpai").val();
  let fenlei = $("#fenlei").val();
  let storepirce = $("#storepirce").val();
  let kuncun = $("#kuncun").val();
  storeguanli.push({
    id: id,
    storename: storename,
    storepinpai: storepinpai,
    fenlei: fenlei,
    storepirce: storepirce,
    kuncun: kuncun,
  });
}
//添加绑定事件
$("#addbtn").click(function () {
  $(".modalAdd").show();
  $("#show").css("display", "none");
  $("#tijiao").off("click");
  $("#tijiao").click(function () {
    layer.confirm(
      "确认提交？",
      {
        btn: ["确认", "取消"], //按钮
      },
      function () {
        $(".modalAdd").hide();
        $("#show").css("display", "block");
        Addstore();
        BindPage(storeguanli);
        // AddPageEvent();
        layer.msg("添加成功！", { icon: 1, time: 500 });
      }
    );
  });
  $("#quxiao").click(function () {
    $(".modalAdd").hide();
    $("#show").css("display", "block");
  });
  $("#fanhui").click(function () {
    $(".modalAdd").hide();
    $("#show").css("display", "block");
  });
});

//删除
$("#userTable").on("click", ".cc", function () {
  let id = $(this).attr("data-id");
  layer.confirm(
    "确认删除？",
    {
      title: "删除",
      btn: ["确认", "取消"],
    },
    function () {
      let storeguanlipageSize = 5; //每页显示条数
      let TotalPage = Math.ceil(storeguanli.length / storeguanlipageSize);
      for(let i=0;i<TotalPage;i++){
          let Addstorea=$(".pageBut").eq(i).attr("class");
          let Addas=Addstorea.split(" ");
          if(Addas[1]=="cyan") {
             var pagesname= $(".pageBut").eq(i).html();
          }
      //    console.log(Addas); 
      }
      let newuser = storeguanli.filter(function (item) {
        return item.id != id;
      });
      storeguanli = newuser;
      BindPage(storeguanli);
      addUser("storeguanli",storeguanli);
      xuanran(storeguanli,storeguanlipageSize,pagesname);
      layer.msg("已删除", { icon: 1, time: 500 });
    },
    function () {
      layer.msg("已取消", { icon: 2, time: 500 });
    }
  );
});
//全选
function AllChoose() {
  $(".All").click(function () {
    let choose = $(".All").prop("checked");
    $(".cbxAll").prop("checked", choose);
  });
}
//批量删除
$("#BtnAll").click(function () {
  let storeguanlipageSize = 5; //每页显示条数
  let TotalPage = Math.ceil(storeguanli.length / storeguanlipageSize);
  for(let i=0;i<TotalPage;i++){
      let Addstorea=$(".pageBut").eq(i).attr("class");
      let Addas=Addstorea.split(" ");
      if(Addas[1]=="cyan") {
         var pagesname= $(".pageBut").eq(i).html();
      }
  //    console.log(Addas); 
  }
  var arr = $(".cbxAll:checked").get();
  layer.confirm(
    "确认全部删除？",
    {
      btn: ["确认", "取消"],
    },
    function () {
      arr.forEach((v) => {
        storeguanli.forEach((el, i, m) => {
          if (el.id == $(v).attr("data-id")) {
            m.splice(i, 1);
          }
        });
      });
      $(".All").prop("checked", false);
      BindPage(storeguanli);
      // AddPageEvent();
      addUser("storeguanli",storeguanli);
      xuanran(storeguanli,storeguanlipageSize,pagesname);
      layer.msg("已删除", {
        icon: 1,
        time: 500,
      });
    },
    function () {
      layer.msg("已取消", {
        icon: 2,
        time: 500,
      });
    }
  );
});

//编辑
function Addmanagement(id){
  var id = $(".id").val();
  var storename = $(".storename").val();
  var fenlei = $(".fenlei").val();
  var storepinpai = $(".storepinpai").val();
  var storepirce = $(".storepirce").val();
  var kuncun = $(".kuncun").val();
  for (var i = 0; i < storeguanli.length; i++) {
    if (id == storeguanli[i].id) {
      storeguanli[i].id = id;
      storeguanli[i].storename = storename;
      storeguanli[i].fenlei = fenlei;
      storeguanli[i].storepinpai = storepinpai;
      storeguanli[i].storepirce = storepirce;
      storeguanli[i].kuncun = kuncun;
      $("input:text").val("");
    }
}
}
$("#userTable").on("click", ".xiugai", function () {
  let storeguanlipageSize = 5; //每页显示条数
    let TotalPage = Math.ceil(storeguanli.length / storeguanlipageSize);
    for(let i=0;i<TotalPage;i++){
        let Addstorea=$(".pageBut").eq(i).attr("class");
        let Addas=Addstorea.split(" ");
        if(Addas[1]=="cyan") {
           var pagesname= $(".pageBut").eq(i).html();
        }
    }
  var id = $(this).attr("data-id");
  var obj = storeguanli.find((v) => v.id == id);
  $(".id").val(obj.id);
  $(".storename").val(obj.storename);
  $(".fenlei").val(obj.fenlei);
  $(".storepinpai").val(obj.storepinpai);
  $(".storepirce").val(obj.storepirce); 
   $(".kuncun").val(obj.kuncun);
  xuanran(storeguanli,5,1);
// $(".xiugai").click(function () {
    
  $(".modalUpdate").show();
  $("#show").css("display", "none");
  $("#tijiao1").off("click");
  $("#tijiao1").click(function () {
    layer.confirm(
      "确认提交？",{
        btn: ["确认", "取消"], //按钮
      },function (){
       $(".modalUpdate").hide();
        $("#show").css("display", "block");
        BindPage(storeguanli);
        Addmanagement(id)
        // AddPageEvent();
        addUser("storeguanli",storeguanli);
        xuanran(storeguanli,storeguanlipageSize,pagesname);
        layer.msg("提交成功", { icon: 1, time: 500 });
        });
  })
  $("#quxiao1").click(function () {
    $(".modalUpdate").hide();
    $("#show").css("display", "block");
  });
  $("#fanhui1").click(function () {
    $(".modalUpdate").hide();
    $("#show").css("display", "block");
  });
});


//查询
$("#queryMem").on("click", function () {
  let storeguanlipageSize = 5; //每页显示条数
  let TotalPage = Math.ceil(storeguanli.length / storeguanlipageSize);
  for(let i=0;i<TotalPage;i++){
      let Addstorea=$(".pageBut").eq(i).attr("class");
      let Addas=Addstorea.split(" ");
      if(Addas[1]=="cyan") {
         var pagesname= $(".pageBut").eq(i).html();
      }
  //    console.log(Addas); 
  }
  Query = [];
  let text1 = $("#text1").val().trim();
  let text4 = $("#text4").val().trim();
  if (!text1 && !text4) return xuanran(parent.storeguanli);
  Query = parent.storeguanli.filter((chaxun) => {
    if (chaxun.storename == text1 || chaxun.storepirce == text4) {
      return chaxun;
    }
  });
  BindPage(Query);
  // AddPageEvent();
  xuanran(Query,storeguanlipageSize,pagesname);
});

$("#userTable").on("mouseover", ".line", function () {
  $(this).css("background-color", "#e8e8e8");
  $("#userTable").on("mouseout", ".line", function () {
    $(this).css("background-color", "white");
  });
});


function pagenumerevent(pagenuber) {
  $(pagenuber).click(function() {
      let storeguanlipageSize = 5; //每页显示条数
      let TotalPage = Math.ceil(storeguanli.length / storeguanlipageSize);
      for(let i=0;i<TotalPage;i++){
          let Addstorea=$(".pageBut").eq(i).attr("class");
          let Addas=Addstorea.split(" ");
          if(Addas[1]=="cyan") {
             var pagesname= $(".pageBut").eq(i).html();
          }
      //    console.log(Addas); 
      }
      if (pagenuber == '#pagDow') {
          pagesname--;
          for (let i = 0; i < TotalPage; i++) {
              let buttoncontent = parseInt($('.pageBut').eq(i).text());
              if (buttoncontent == pagesname) {
                  $('.pageBut').eq(i).addClass("cyan").siblings().removeClass("cyan");
              }
          }
      } else if (pagenuber == '#pagUp') {
          pagesname++;
          for (let i = 0; i < TotalPage; i++) {
              let buttoncontent = parseInt($('.pageBut').eq(i).text());
              if (buttoncontent == pagesname) {
                  $('.pageBut').eq(i).addClass("cyan").siblings().removeClass("cyan");
              }
          }
      }
      if (pagesname <= 0) {
          pagesname = 1;
          $('.pageBut').eq(0).addClass("cyan").siblings().removeClass("cyan");
      } else if (pagesname >= TotalPage) {
          pagesname = TotalPage;
      }
      xuanran(storeguanli,storeguanlipageSize,pagesname);
  })
}
pagenumerevent('#pagUp');//上一页
pagenumerevent('#pagDow');//下一页